package es.project.sien7e.main.services;

import java.sql.SQLException;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.validation.Valid;

import org.springframework.stereotype.Service;

import es.project.sien7e.main.PMF;
import es.project.sien7e.main.model.Rolmenu;

@Service
public class RolMenuServiceImpl implements RolMenuService {

	@SuppressWarnings("unchecked")
	@Override
	public List<Rolmenu> getRolmenus() throws SQLException {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(Rolmenu.class);
		return (List<Rolmenu>) query.execute();
	}

	@Override
	public Rolmenu insertaRolmenu(@Valid Rolmenu obj) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Rolmenu obj2 = null;
		if (obj.getRol() == 0)
			obj.setRol(null);
		if (obj.getMenu() == 0)
			obj.setMenu(null);
		try {
			obj2 = pm.makePersistent(obj);
		} finally {
			pm.close();
		}
		return obj2;
	}

	@Override
	public Integer borrarRolmenu(@Valid Rolmenu obj) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Integer val = 0;
		try {
			pm.deletePersistent(obj);
			val = 1;
		} finally {
			pm.close();
		}
		return val;
	}
}
